<?php


namespace app\superadmin\controller;


use app\superadmin\model\User;
use Mpdf\Mpdf;
use think\Db;
use think\facade\Request;

/**
 * Class Setting 設定
 * @package app\superadmin\controller
 */
class Setting extends LoggingBase
{
    #region 界面
    /**
     * 系統設定
     */
    public function SystemSetting()
    {
        $SysData = Db::table('system')->where('id', 1)->find();
        if (!$SysData) {
            $SysData = array(
                'SystemName' => '',
                'Contact' => '',
                'ContactNumber' => '',
                'ContactAddress' => '',
                'ContactEmail' => ''
            );
        }
        $this->assign('SysData', $SysData);
        return $this->mb_Display('系统设定', 'SystemSetting');
    }

    /**
     * 收款設定
     */
    public function VmqSetting()
    {
        $VmqData = Db::table('system')->where('id', 1)->find();
        if (!$VmqData) {
            $VmqData = array(
                'VmqKey' => '',
                'VmqHost' => '',
            );
        }
        $this->assign('VmqData', $VmqData);
        return $this->mb_Display('收款设定', 'VmqSetting');
    }

    /**
     * 個人設定
     */
    public function UserSetting()
    {
        $this->assign('UserData', $this->m_User);
        return $this->mb_Display('个人设定', 'UserSetting');
    }

    public function ResetPassword()
    {
        return $this->mb_Display('修改密码', 'ResetPassword');
    }

    #endregion


    #region 接口

    /**
     * 提交系統設置
     */
    public function mb_SubmitSystemSetting()
    {
        $SystemName = Request::has('SystemName') ? addslashes(Request::param('SystemName')) : '';
        $Contact = Request::has('Contact') ? addslashes(Request::param('Contact')) : '';
        $Number = Request::has('Number') ? addslashes(Request::param('Number')) : '';
        $Address = Request::has('Address') ? addslashes(Request::param('Address')) : '';
        $Email = Request::has('Email') ? addslashes(Request::param('Email')) : '';

        Db::startTrans();
        try {
            $Data = array(
                'SystemName' => $SystemName,
                'Contact' => $Contact,
                'ContactNumber' => $Number,
                'ContactAddress' => $Address,
                'ContactEmail' => $Email
            );
            if (Db::table('system')->where('ID', 1)->find() == null) {
                Db::table('system')->insert($Data);
            } else {
                Db::table('system')->where('ID', 1)->data($Data)->update();
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal, '保存成功');
    }

    /**
     * 提交收款設置
     */
    public function mb_SubmitVmqSetting()
    {
        $VmqKey = Request::has('VmqKey') ? addslashes(Request::param('VmqKey')) : '';
        $VmqHost = Request::has('VmqHost') ? addslashes(Request::param('VmqHost')) : '';

        Db::startTrans();
        try {
            $Data = array(
                'VmqKey' => $VmqKey,
                'VmqHost' => $VmqHost
            );
            Db::table('system')->where('ID', 1)->data($Data)->update();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal, '保存成功');
    }

    /**
     * 提交
     */
    public function mb_SubmitUserSetting()
    {
        $Nick = Request::has('Nick') ? addslashes(Request::param('Nick')) : '';
//        $Pass = Request::has('Pass') ? addslashes(Request::param('Pass')) : '';

        $Sex = Request::has('Sex') ? intval(Request::param('Sex')) : 0;
        $WeChat = Request::has('WeChat') ? addslashes(Request::param('WeChat')) : '';
        $QQ = Request::has('QQ') ? addslashes(Request::param('QQ')) : '';
        $Phone = Request::has('Phone') ? addslashes(Request::param('Phone')) : '';
        $Email = Request::has('Email') ? addslashes(Request::param('Email')) : '';

        Db::startTrans();
        try {

            $this->m_User->Nick = $Nick;
//            $this->m_User->EncPass = md5($Pass);
//            $this->m_User->BasePass = $Pass;
            $this->m_User->WeChat = $WeChat;
            $this->m_User->QQ = $QQ;
            $this->m_User->Phone = $Phone;
            $this->m_User->Email = $Email;
            $this->m_User->Sex = $Sex;
            $this->m_User->save();

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal, '保存成功');
    }

    public function mb_SubmitResetPassword()
    {
        $OldPass = Request::has('OldPass') ? addslashes(Request::param('OldPass')) : '';
        $NewPass = Request::has('NewPass') ? addslashes(Request::param('NewPass')) : '';

        if($this->m_User->BasePass != $OldPass){
            mb_OutMessage(ErrCode_Toast, '旧密码不正确');
        }

        Db::startTrans();
        try {
            $this->m_User->EncPass = md5($NewPass);
            $this->m_User->BasePass = $NewPass;
            $this->m_User->save();

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            mb_OutMessage(ErrCode_Toast, $e->getMessage());
        }
        mb_OutMessage(ErrCode_Normal, '保存成功');
    }
    #endregion
}